package com.taotao.controller;

import com.taotao.EasyUIDataGridResult;
import com.taotao.TaotaoResult;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.service.ItemParamItemService;
import com.taotao.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * Created by mafuhua on 2017/9/20.
 */
@Controller
public class ItemController {
    @Autowired
    private ItemService itemService;

    @RequestMapping("/item/{itemId}")
    @ResponseBody
    private TbItem getItemById(@PathVariable Long itemId) {
        TbItem tbItem = itemService.getItemById(itemId);
        System.out.println(tbItem);
        return tbItem;
    }

    @RequestMapping("/item/list")
    @ResponseBody
    private EasyUIDataGridResult getItemList(@RequestParam(defaultValue="1") Integer page,
                                             @RequestParam(defaultValue="30") Integer rows) {
        System.out.println("hhhhhhhhhhh");
        EasyUIDataGridResult itemList = itemService.getItemList(page, rows);
        System.out.println(itemList);
        return itemList;
    }

    @RequestMapping(value="/item/save", method= RequestMethod.POST)
    @ResponseBody
    private TaotaoResult addItem(TbItem item,String desc,String itemParams ) {
        TbItemDesc itemDesc = new TbItemDesc();
        itemDesc.setItemDesc(desc);
        TaotaoResult taotaoResult = itemService.addItem(item, itemDesc, itemParams);
        return taotaoResult;
    }

    @Autowired
    private ItemParamItemService itemParamItemService;

    @RequestMapping("/page/item/{itemId}")
    private String showItemParam(@PathVariable long itemId, Model model) {
        String itemParemById = itemParamItemService.getItemParemById(itemId);
        model.addAttribute("html", itemParemById);
        return "item-param";
    }

}
